<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'赠品活动管理'),
	array('op'=>'edit','title'=>'添加赠品活动'),
	array('op'=>'log','title'=>'赠送记录'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $fields=array('id','title','image','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_goods_gift',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'赠品标题','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),

			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),			
		)),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_goods_gift',array('id'=>$id));
	$item['onlinetime']=json_decode($item['onlinetime'],true);
	$item['gifts']=json_decode($item['gifts'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['activity']=array('1'=>'指定商品','0'=>'订单金额');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'赠品设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'赠品管理'),
			array('op'=>'edit','title'=>'添加赠品'),
		),
		'edit'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),			
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'activity','title'=>'活动类型','type'=>'radio','options'=>'activity','help'=>''),
			array('name'=>'orderprice','title'=>'订单金额','type'=>'string','show'=>array('activity'=>0),'help'=>'订单金额达到设置的金额才赠送！如果设置了多个赠品活动，则以订单金额满足的所有活动中，条件最高的赠品活动为准'),
			array('type'=>'choose','title'=>'指定商品','name'=>'goodsid','action'=>'jy_weishop/goods.get_goods/','choose_title'=>'指定商品','show'=>array('activity'=>1),'help'=>'购买设置的商品才赠送！'),
			array('type'=>'choose_many','title'=>'选择赠品','name'=>'gifts','action'=>'jy_weishop/goods.get_goods/many','choose_title'=>'选择赠品'),
			array('name'=>'cangetnum','title'=>'限赠个数','type'=>'string','help'=>'每个用户限领赠品总数，默认不限'),
			array('name'=>'onlinetime','title'=>'限时设置','type'=>'time_range'),
			array('name'=>'share_title','title'=>'分享标题','type'=>'string'),
			array('name'=>'share_desc','title'=>'分享描述','type'=>'string'),			
			array('name'=>'share_icon','title'=>'分享图片','type'=>'image'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch'),	
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('title','status','paixu','goodsid','image','activity','orderprice','gifts','onlinetime','cangetnum','share_title','share_icon','share_desc'),$postdata);
	$data['onlinetime']=ijson_encode($data['onlinetime']);
	$data['gifts']=$data['gifts']?ijson_encode($data['gifts']):'';
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_goods_gift',$data,array('id'=>$id));
				exi('设置成功！','','list');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_weishop_goods_gift',$data);
				exi('添加成功！','','list');
			}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_weishop_goods_gift',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_goods_gift',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_goods_gift',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='log'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $fields=array();
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_weishop_order_gift',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		foreach($list as &$item){
			$item['member']=model('core/member')->member_show($item['mid']);
			$item['ordersn']=$this->pdo_getcolumn('order',array('id'=>$item['order_id']),'ordersn');
			$item['gift']=$this->pdo_getcolumn('goods_gift',array('id'=>$item['gift_id']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'赠送会员','type'=>'member'),
			array('name'=>'ordersn','title'=>'关联订单号','type'=>'string'),
			array('name'=>'gift','title'=>'赠送活动名称','type'=>'string'),
			array('name'=>'createtime','title'=>'赠送时间','type'=>'time'),			
		)),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
?>